home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Apple Developer Connection Student Program
/
ADC Tools Sampler CD Disk 3 1999.iso
/
Documentation
/
Books
/
Learn Java on the Macintosh
/
Learn Java Projects
/
11.04 - circle at click
/
SimpleDraw.java
< prev
next >
Wrap
Text File
|
1996-04-22
|
2KB
|
64 lines
/* -------------------------------------------------------------
This applet paints a red circle wherever you click.
Java's classes: Applet (applet)
Event (awt) user-generated action
Graphics (awt) used for drawing
Color (awt) defines colors
Custom classes: SimpleDraw
Circle defines and draws circles
------------------------------------------------------------- */
import java.applet.Applet;
import java.awt.*;
public class SimpleDraw extends Applet {
Circle c;
/** Create a circle to start with. */
public void init() {
c = new Circle();
c.initialize(50, 50);
}
/** Create a new red circle when the user clicks the mouse. */
public boolean mouseUp(Event e, int x, int y) {
c = new Circle();
c.initialize(x, y);
repaint();
return true;
}
/** Repaint the newest circle. */
public void paint(Graphics g) {
c.draw(g);
}
}
/** Maintain circle information and provide drawing capabilities. */
class Circle {
Color color;
int x;
int y;
/** Draw a circle that is 20 pixels in radius. */
void draw(Graphics g) {
g.setColor(this.color);
g.fillOval(this.x - 20, this.y - 20, 40, 40);
}
/** Initialize a red circle at the given pixel location. */
void initialize(int x, int y) {
color = Color.red;
this.x = x;
this.y = y;
}
}